var express = require('express');
var router = express.Router();
var connection=require("../db/index");

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

//获取swiperlist
router.get('/swiperList', function(req, res, next) {
  connection.query('select * from swiperlists', function (error, results, fields) {
    if (error) throw error;
    res.json({
      results
    })
  });
});


//获取iconlists
router.get('/iconsList', function(req, res, next) {
  connection.query('select * from iconlists', function (error, results, fields) {
    if (error) throw error;
    res.json({
      results
    })
  });
});

//获取mainImgs
router.get('/mainImgs', function(req, res, next) {
  connection.query('select * from mainimgs', function (error, results, fields) {
    if (error) throw error;
    res.json({
      results
    })
  });
});

//获取recommendslist 下拉刷新截取数据
router.get('/recommendslist', function(req, res, next) {
  
  let index=req.query.index || 0; //下标  活的值
  let listLength=req.query.listLength || 5;  //数据长度  死的值

  connection.query(`select * from recommendslist limit ${index},${listLength}`, function (error, results, fields) {
    if (error) throw error;
    res.json({
      results
    })
  });
});

//获取searchsList
router.get('/searchsList', function(req, res, next) {
	 let searchData = require("../mock/data.json");
    res.json({
   	 	searchData
    })

});

 //搜索功能接口
 router.get('/searchmsg', function(req, res, next) {

	let val = req.query.q;

	connection.query('select * from searchmsg where name like "%'+val+'%"', function (error, results, fields) {
	  	res.json({
	  		results
	  	})
	});

});


//加入购物车  use 包括了post 和 get
router.use('/shopcars', function(req, res, next) {
 
  let good_id=req.body.id;
  let good_title=req.body.title;
  let good_price=req.body.price;
  let good_img=req.body.img;
  let good_count="1";
   //console.log(good_id)
	connection.query(`select * from shopcars where good_id=${good_id}`, function (error, results, fields) {
     //console.log(good_id)

	  	if(results[0]){  //购物车数据里有
         good_count+=1;
         connection.query(`update shopcars set good_count=${good_count} where good_id=${good_id}`, function (error, results, fields) {
        
          if(error){
           res.json({
               code:0,
               msg:"添加失败",
           })
         }else{
          res.json({
              code:1,
              msg:"添加成功",
              shopCars:results
          })
         }
       });
     }else{  //购物车数据里没有
        
        let msgs=`insert into shopcars (good_id,good_title,good_price,good_count,good_img) values (?,?,?,?,?)`

        connection.query(msgs,[good_id,good_title,good_price,good_count,good_img], function (error, results, fields) {
        
          if(error){
           res.json({
               code:0,
               msg:"添加失败",
           })
         }else{
          res.json({
              code:1,
              msg:"添加成功",
              shopCars:results
          })
         }
       });
     }
	});

});



module.exports = router;
